package com.qf.controller;

import com.qf.constats.Status;
import com.qf.model.User;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("system")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("findAll")
    public List<User> findAll(){
        List<User> list = userService.GetAll();
        return list;
    }
    @PostMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult result =  userService.findPage(queryPageBean);
        return result;
    }
    @RequestMapping("add")
    public Result add(@RequestBody User user){
        int row = userService.addUser(user);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @GetMapping("findById")
    public Result findById(@RequestParam("id") int id){
       User user = userService.findById(id);
        Result result = new Result();
        result.setMessage(Status.FAIL.getMessage());
        if (ObjectUtils.isNotEmpty(user)){
            result.setFlag(true);
            result.setData(user);
            result.setMessage(Status.SUCCESS.getMessage());
        }
        return result;

    }

    @PostMapping("edit")
    public Result edit(@RequestBody User user){
        int row  =  userService.edit(user);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    @GetMapping("delete")
    public Result delete(@RequestParam("id") Integer id){
        int row =  userService.deleteById(id);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
